package com.cqq.common.core.bean;

import java.util.List;

import com.cqq.common.core.base.BaseBean;

/**
 * <h3>分页信息 Bean</h3>
 *
 * <p>
 * 用于服务方法对分页查询结果对象的封装。
 * </p>
 * 
 * @author xijq
 * @version 1.0.0
 * 
 */
public class PageInfoBean<T> extends BaseBean {

	/**
	 * 默认每页记录数常量
	 */
	public static final Integer DEFAULT_PAGE_SIZE = 20;
	
	private static final long serialVersionUID = -4258127433164986450L;

	/**
	 * 当前页码
	 */
	private Integer pageNo=1;
	
	/**
	 * 每页记录数
	 */
	private Integer pageSize=DEFAULT_PAGE_SIZE;
	
	/**
	 * 每页数据列表对象
	 */
	private List<T> data;
	
	/**
	 * 总页数
	 */
	private Integer totalPages=0;
	
	/**
	 * 总记录数
	 */
	private Integer totalRecords=0;

	/**
	 * <h3>默认构造分页信息对象</h3>
	 * 
	 */
	public PageInfoBean(){
	}
	
	/**
	 * <h3>按分页数据和总记录数构造分页信息对象</h3>
	 * 
	 * @param data 分页列表数据
	 * @param totalRecords 总记录数
	 * 
	 */
	public PageInfoBean(List<T> data, Integer totalRecords){
		this.setData(data);
		this.setTotalRecords(totalRecords);
	}

	/**
	 * <h3>按分页数据、总记录数、每页记录数构造分页信息对象</h3>
	 * 
	 * @param data 分页列表数据
	 * @param totalRecords 总记录数
	 * @param pageSize 每页记录数
	 * 
	 */
	public PageInfoBean(List<T> data, Integer totalRecords, Integer pageSize){
		this.setData(data);
		this.setPageSize(pageSize);
		this.setTotalRecords(totalRecords);
	}

	/**
	 * <h3>按分页数据、总记录数、每页记录数构造分页信息对象</h3>
	 * 
	 * @param data 分页列表数据
	 * @param totalRecords 总记录数
	 * @param pageSize 每页记录数
	 * @param pageNo 当前页码
	 * 
	 */
	public PageInfoBean(List<T> data, Integer totalRecords, Integer pageSize, Integer pageNo){
		this.setData(data);
		this.setPageSize(pageSize);
		this.setPageNo(pageNo);
		this.setTotalRecords(totalRecords);
	}
	
	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		if(pageNo==null || pageNo<=0){
			return;
		}
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		if(pageSize==null || pageSize<=0){
			return;
		}		
		this.pageSize = pageSize;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public Integer getTotalRecords() {
		return totalRecords;
	}

	public void setTotalRecords(Integer totalRecords) {
		if(totalRecords==null || totalRecords<0){
			return;
		}
		this.totalRecords = totalRecords;
		this.totalPages = totalRecords % pageSize == 0 ? totalRecords / pageSize : totalRecords / pageSize + 1;
	}
	
	/**
	 * <h3>获取总页数</h3>
	 * 
	 * @return int
	 * 
	 */
	public Integer getTotalPages() {
		return totalPages;
	}
	
	/**
	 * <h3>获取第一页</h3>
	 * 
	 * @return int
	 * 
	 */
	public Integer getFirstPage() {
		return 1;
	}

	/**
	 * <h3>获取最后一页</h3>
	 * 
	 * @return int
	 * 
	 */
	public Integer getLastPage() {
		return this.getTotalPages();
	}

	/**
	 * <h3>获取上一页码</h3>
	 * 
	 * @return int
	 * 
	 */
	public Integer getPreviousPage() {
		return this.pageNo > 1 ? this.pageNo - 1 : 1;
	}

	/**
	 * <h3>获取下一页码</h3>
	 * 
	 * @return int
	 * 
	 */
	public Integer getNextPage() {
		return this.totalPages > this.pageNo ? this.pageNo+1 : this.totalPages;
	}

	/**
	 * <h3>获取当前页码</h3>
	 * 
	 * @return int
	 * 
	 */
	public Integer getCurrentPage() {
		return this.pageNo;
	}

	/**
	 * <h3>是否是第一页</h3>
	 * 
	 * @return true or false
	 * 
	 */
	public boolean isFirstPage() {
		return this.pageNo == 1 ? true : false;
	}

	/**
	 * <h3>是否是最后一页</h3>
	 * 
	 * @return true or false
	 * 
	 */
	public boolean isLastPage() {
		return this.pageNo == this.totalPages ? true : false;
	}
	
}
